//
//  CommissionFinalController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/3/17.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "CommissionFinalController.h"
#import "CommissionFinalCell.h"
#import "CommissionFinalModel.h"
#import "TopStatusView.h"

@interface CommissionFinalController ()<UITableViewDelegate,UITableViewDataSource>
{
    MBProgressHUD *progressHUD;
    NSUInteger _refreshIndex;
    UIImageView *noDrawImage;
}

@property(nonatomic,strong)NSMutableArray *commissionFinalArray;

@property(nonatomic,strong)UITableView *commissionFinalView;

@end

@implementation CommissionFinalController

static NSString *const commissionFinalIdentifier = @"commissionFinalIdentifier";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    _refreshIndex = 1;
    [self commissionFinalListData:_refreshIndex];
    [self buildView];
}

#pragma mark >>>>>>>>刷新
- (void)commissionFinalListLoadMoreData
{
    _refreshIndex ++;
    [self commissionFinalListData:_refreshIndex];
    [self.commissionFinalView.mj_footer endRefreshing];
}


- (void)commissionFinalListData:(NSInteger)refreshIndex
{
    progressHUD = [MBProgressHUD loadingInformationToView];
    [self.view addSubview:progressHUD];
    
    if (refreshIndex == 1) {
        [self.commissionFinalArray removeAllObjects];
    }
    
    NSString *refreshIndexStr = [NSString stringWithFormat:@"%zd",refreshIndex];
    
    
    @weakify(self);
    NSDictionary *params = @{@"username":USERNAME,@"page":refreshIndexStr};
    NSLog(@"%@",params);
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/Expand/commissionCashList" completeBlock:^(id object) {
        
        @strongify(self);
        if (JudgeBackDataCode) {
            
            NSArray *dataArray = [object objectForKey:@"data"];
            for (NSDictionary *dict in dataArray) {
                CommissionFinalModel *commissionFinalModel = [[CommissionFinalModel alloc] initWithDictionary:dict error:nil];
                
                if (commissionFinalModel) {
                    [self.commissionFinalArray addObject:commissionFinalModel];
//                    NSLog(@"%@",commissionFinalModel);
                }
            
            }
        }else{
            
            //订单没有
            if (refreshIndex == 1) {
                noDrawImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_NoRecord"]];
                [self.view addSubview:noDrawImage];
                
                [noDrawImage mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(self.view.mas_centerX);
                    make.centerY.equalTo(self.view.mas_centerY).offset(-HEIGHT/8);
                    make.width.mas_equalTo(WIDTH/1.5);
                    make.height.mas_equalTo(WIDTH/2.4);
                }];
                
                [self.commissionFinalView removeFromSuperview];
                
            }else{
                
                //没有更多数据
                [self.commissionFinalView.mj_footer endRefreshingWithNoMoreData];
                
            }
            
            
        }
        [progressHUD hide:YES];
        [self.commissionFinalView reloadData];
        
    }];
    
}



#pragma mark 数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.commissionFinalArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CommissionFinalCell *cell=[tableView dequeueReusableCellWithIdentifier:commissionFinalIdentifier];
    
    if (cell==nil) {
        cell=[[CommissionFinalCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:commissionFinalIdentifier];
    }
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    
    CommissionFinalModel *commissionFinalModel = self.commissionFinalArray[indexPath.row];
    
    //变化佣金
    cell.operatorLabel.text = commissionFinalModel.operator_commission;
    //剩余佣金
    cell.currentLabel.text = commissionFinalModel.current_commission;
    //状态
    if ([commissionFinalModel.status isEqualToString:@"0"]) {
        cell.statusLabel.text = @"审核中";
    }else if ([commissionFinalModel.status isEqualToString:@"1"]){
        cell.statusLabel.text = @"结算完成";
    }else{
        cell.statusLabel.text = @"审核不通过";
    }
    
    //时间
    cell.create_timeLabel.text = commissionFinalModel.create_time;
    
    if ([commissionFinalModel.update_time isEqualToString:@"0000-00-00 00:00:00"]) {
        cell.update_timeLabel.text = @"-----";
    }else{
        cell.update_timeLabel.text = commissionFinalModel.update_time;
    }
    
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}


- (void)buildView
{
    self.title = @"佣金结算记录";
    TopStatusView *topStatusView = [[TopStatusView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 50) titleArray:@[@"变化佣金",@"剩余佣金",@"结算状态",@"申请时间",@"结算时间"]];
    [self.view addSubview:topStatusView];
    self.view.backgroundColor = BACKGROUND_COLOR;
 
    self.commissionFinalView = [[UITableView alloc] initWithFrame:CGRectMake(0,50,WIDTH, HEIGHT -64-50 ) style:UITableViewStylePlain];
    [self.view addSubview:_commissionFinalView];
    self.commissionFinalView.delegate = self;
    self.commissionFinalView.dataSource = self;
    self.commissionFinalView.backgroundColor = BACKGROUND_COLOR;
    self.commissionFinalView.rowHeight = 45;
    [self.commissionFinalView registerNib:[UINib nibWithNibName:@"CommissionFinalCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:commissionFinalIdentifier];
    
    self.commissionFinalView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(commissionFinalListLoadMoreData)];
    
  
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (NSMutableArray *)commissionFinalArray
{
    if (!_commissionFinalArray) {
        _commissionFinalArray = [NSMutableArray array];
    }
    return _commissionFinalArray;
}


@end
